﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Linq.Expressions;

namespace FoundationLib.Web.Mvc
{
	/// <summary>
	/// Provides helper methods for text boxes.
	/// </summary>
	public static class TextBoxHelper
	{
		/// <summary>
		/// Creates a short date text box.
		/// </summary>
		/// <typeparam name="TModel">The model type</typeparam>
		/// <typeparam name="TProperty">The returing type of the model property</typeparam>
		/// <param name="helper">The HTML helper</param>
		/// <param name="property">The property expression</param>
		/// <param name="htmlAttributes">A set of HTML attributes</param>
		/// <returns>A HTML string defining the text box</returns>
		public static MvcHtmlString DateBoxFor<TModel, TProperty>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TProperty>> property,
			object htmlAttributes)
		{
			object rawValue = property.Compile().Invoke(helper.ViewData.Model);
			DateTime? value = null;

			if (rawValue is DateTime)
				value = (DateTime)rawValue;
			else if (rawValue is DateTime?)
				value = rawValue as DateTime?;

			string date = value.HasValue ? value.Value.ToShortDateString() : String.Empty;
			string propertyName = ExpressionHelper.PropertyPathFor(property);

			return helper.TextBox(propertyName, date, htmlAttributes);
		}

		/// <summary>
		/// Creates a short date text box.
		/// </summary>
		/// <typeparam name="TModel">The model type</typeparam>
		/// <typeparam name="TProperty">The returing type of the model property</typeparam>
		/// <param name="helper">The HTML helper</param>
		/// <param name="property">The property expression</param>
		/// <returns>A HTML string defining the text box</returns>
		public static MvcHtmlString DateBoxFor<TModel, TProperty>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TProperty>> property)
		{
			return DateBoxFor(helper, property, null);
		}

		/// <summary>
		/// Creates a time text box.
		/// </summary>
		/// <typeparam name="TModel">The model type</typeparam>
		/// <typeparam name="TProperty">The returing type of the model property</typeparam>
		/// <param name="helper">The HTML helper</param>
		/// <param name="property">The property expression</param>
		/// <param name="htmlAttributes">A set of HTML attributes</param>
		/// <returns>A HTML string defining the text box</returns>
		public static MvcHtmlString TimeBoxFor<TModel, TProperty>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TProperty>> property,
			object htmlAttributes)
		{
			object rawValue = property.Compile().Invoke(helper.ViewData.Model);
			TimeSpan? value = null;

			if (rawValue is TimeSpan)
			{
				value = (TimeSpan)rawValue;
			}
			else if (rawValue is TimeSpan?)
			{
				value = rawValue as TimeSpan?;
			}
			else if (rawValue is DateTime)
			{
				value = ((DateTime)rawValue).TimeOfDay;
			}
			else if (rawValue is DateTime?)
			{
				DateTime? date = rawValue as DateTime?;
				rawValue = date.HasValue ? date.Value.TimeOfDay : new TimeSpan?();
			}

			string time = value.HasValue ? value.Value.ToString(@"hh\:mm") : String.Empty;
			string propertyName = ExpressionHelper.PropertyPathFor(property);

			return helper.TextBox(propertyName, time, htmlAttributes);
		}

		/// <summary>
		/// Creates a time text box.
		/// </summary>
		/// <typeparam name="TModel">The model type</typeparam>
		/// <typeparam name="TProperty">The returing type of the model property</typeparam>
		/// <param name="helper">The HTML helper</param>
		/// <param name="property">The property expression</param>
		/// <returns>A HTML string defining the text box</returns>
		public static MvcHtmlString TimeBoxFor<TModel, TProperty>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TProperty>> property)
		{
			return TimeBoxFor(helper, property, null);
		}
	}
}
